﻿//using AutoMapper;
//using AutoMapper.QueryableExtensions;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Linq.Expressions;
//using System.Text;
//using System.Threading.Tasks;

//namespace DDD.Infrastructure.Extend
//{
//    /// <summary>
//    /// AutoMapper扩展方法
//    /// </summary>
//    public static class AutoMapperExtensions
//    {


//        /// <summary>
//        /// 类型映射
//        /// </summary>
//        /// <typeparam name="TSource">源对象</typeparam>
//        /// <typeparam name="TTarget">目标对象</typeparam>
//        /// <param name="source"></param>
//        /// <param name="target"></param>
//        /// <returns></returns>
//        public static TTarget MapTo<TSource, TTarget>(this TSource source, TTarget target)
//        {
//            source.NotNull(nameof(source));
//            target.NotNull(nameof(target));
//            var config = new MapperConfiguration(ctx => ctx.CreateMap(typeof(TSource), typeof(TTarget)));

//            var mapper = config.CreateMapper();
//            return (TTarget)mapper.Map<TSource, TTarget>(source, target);

//        }




//        /// <summary>
//        /// 对象对对象
//        /// </summary>
//        /// <typeparam name="TResult"></typeparam>
//        /// <param name="self"></param>
//        /// <returns></returns>
//        public static TResult MapTo<TResult>(this object self)
//        {

//            if (self == null)
//            {
//                return default(TResult);
//            }

//            var config = new MapperConfiguration(ctx => ctx.CreateMap(self.GetType(), typeof(TResult)));

//            //Mapper.Initialize(ctx => ctx.CreateMap(self.GetType(), typeof(TResult)).ProjectUsing());

//            var mapper = config.CreateMapper();
//            return (TResult)mapper.Map<TResult>(self);
//        }

//        public static Task<TResult> MapToAsync<TTarget, TResult>(this Task<TTarget> self) where TResult :class,new()
           
//        {
//            var target = self.Result;
//            if (target == null)
//            {
//                return Task.FromResult(default(TResult));
//            }

//            var config = new MapperConfiguration(ctx => ctx.CreateMap(target.GetType(), typeof(TResult)));

//            //Mapper.Initialize(ctx => ctx.CreateMap(self.GetType(), typeof(TResult)).ProjectUsing());

//            var mapper = config.CreateMapper();
//            return Task.FromResult((TResult)mapper.Map<TResult>(target));
//        }

//        /// <summary>
//        /// 转成dto
//        /// </summary>
//        /// <typeparam name="TSource">数据源</typeparam>
//        /// <typeparam name="TDestination">目标</typeparam>
//        /// <param name="source">数据源</param>
//        /// <param name="membersToExpand">过滤</param>
//        /// <returns></returns>

//        public static IQueryable<TDestination> ToOutput<TSource, TDestination>(this IQueryable<TSource> source, params Expression<Func<TDestination, object>>[] membersToExpand)
//        {

//            if (source.IsNull())
//            {
//                return default(IQueryable<TDestination>);
//            }

//            var config = new MapperConfiguration(ctx => ctx.CreateMap<TSource, TDestination>());
//            return source.ProjectTo(config, membersToExpand);

//        }




//    }
//}
